<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$branch_list = pdo_getall('yzzc_sun_branch',['uniacid'=>$_W['uniacid']],['id','name']);
$shuttle_list = pdo_getall('yzzc_sun_shuttle',['uniacid'=>$_W['uniacid']],['id','name']);
$where = "where i.uniacid =" . $_W['uniacid'];
$keywords = $_GPC['keywords'] ?? '';
if ($keywords) {
    $where .= " and (o.username LIKE  '%$keywords%' or o.tel LIKE  '%$keywords%' or o.ordernum LIKE  '%$keywords%')";
}
$branch_id = $_GPC['branch_id'] ?? '';
if ($branch_id){
    $where.= " and i.branch_id=".$branch_id;
}
$shuttle_id = $_GPC['shuttle_id'] ?? '';
if ($shuttle_id){
    $where.= " and i.shuttle_id=".$shuttle_id;
}
$status = $_GPC['status'] ?? 0;
if (isset($_GPC['status']) && $_GPC['status'] !=''){
    $where.=' and i.status='.$_GPC['status'];
}
$type = $_GPC['type'] ?? '';
if ($type){
    $where.=' and i.type='.$type;
}

$range_time = $_GPC['range_time'] ?? [];
$start_time = $end_time = '';
if ($range_time) {
    $start_time = $range_time['start'];
    $end_time = $range_time['end'];
    if ($start_time && $end_time) {
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where .= ' and i.createtime > ' . $start_time . ' and i.createtime<=' . $end_time;
    }
}
$page = max(1, intval($_GPC['page']));
$size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
$sql_page = 'SELECT i.id,i.total,i.type,i.order_id,i.branch_id,i.amount,i.shuttle_id,i.status,i.createtime,i.income_time,i.status,i.order_status,o.ordernum,o.username,o.tel,o.total_money FROM ' . tablename('yzzc_sun_shuttle_income') . " i left join ".tablename('yzzc_sun_order')." o on i.order_id=o.id {$where} ORDER BY i.createtime DESC LIMIT " . (($page - 1) * $size) . ',' . $size;
$info = pdo_fetchall($sql_page);
$search_total = $wait_amount = $finish_amount =  0;
if ($info){
    foreach ($info as $key => $value){
        foreach ($branch_list as $val){
            if ($value['branch_id'] == $val['id']){
                $info[$key]['branch_name'] = $val['name'];
            }
        }
        $shuttle_info = pdo_get('yzzc_sun_shuttle',['id'=>$value['shuttle_id']],['name']);
        $info[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
        $info[$key]['income_time'] = $value['income_time'] ? date('Y-m-d H:i:s',$value['income_time']) : '';
        $info[$key]['shuttle_name'] = $shuttle_info['name'] ?? '';
    }

    //统计
    $sql = 'SELECT i.id,i.total,i.type,i.order_id,i.branch_id,i.amount,i.shuttle_id,i.status,i.createtime,i.income_time,i.status,i.order_status,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_shuttle_income') . " i left join ".tablename('yzzc_sun_order')." o on i.order_id=o.id {$where}";
    $total_count = pdo_fetchall($sql);
    foreach ($total_count as $value){
        if ($value['status'] == 0){
            $wait_amount+=$value['amount'];
        }
        if ($value['status'] == 1){
            $finish_amount+=$value['amount'];
        }
        if ($end_time && $start_time) {
            $search_total += $value['amount'];
        }
    }
}

$total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_shuttle_income") . " where uniacid =" . $_W['uniacid']);
$pager = pagination($total, $page, $size);

if ($_GPC['op'] == 'delete') {
    $is = pdo_get('yzzc_sun_goods', array('sid' => $_GPC['id']));

    if ($is) {
        message('请先至车辆列表下架所有车辆才能删除', '', 'error');
    } else {
        $branch_info = pdo_get('yzzc_sun_branch', array('id' => $_GPC['id']));
        $res = pdo_delete('yzzc_sun_branch', array('id' => $_GPC['id']));
        if ($res) {
            if ($branch_info['type'] != 1){
                pdo_update('yzzc_sun_store',['status'=>0],['store_id'=>$branch_info['store_id']]);
            }
            message('操作成功', $this->createWebUrl('branchslist', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
}

if ($_GPC['op'] == 'income'){
    //单次
    $id = $_GPC['id'];
    $income_info = pdo_get('yzzc_sun_shuttle_income',['id'=>$id]);
    if ($income_info['status'] ==1){
        message('该条收益已经结算了', '', 'error');
    }
    if ($income_info['order_status'] !=2){
        message('该条收益还未还车，不能结算收益', '', 'error');
    }
    $data = [
        'status' => 1,
        'income_time' => time(),
    ];
    $shuttle_info = pdo_get('yzzc_sun_shuttle',['id'=>$income_info['shuttle_id']]);
    pdo_fetchall('START TRANSACTION');//启动一个新事务

    $result = pdo_update('yzzc_sun_shuttle_income',$data,['id'=>$id]);
    $income_info = pdo_update('yzzc_sun_shuttle',['balance'=>$shuttle_info['balance']+$income_info['amount']],['id'=>$income_info['shuttle_id']]);
    if ($result && $income_info){
        pdo_fetchall('COMMIT');//事务提交
        message('结算成功', $this->createWebUrl('shuttle_income', array()), 'success');
    }
    pdo_fetchall('ROLLBACK');//事务提交
    message('结算失败', '', 'error');
}
if ($_GPC['op'] == 'branch_income'){
    $branch_id = $_GPC['branch_id'];
    $range_time = $_GPC['range_time'] ?? [];
    $where = 'status= 1 and branch_id='.$branch_id;
    $type = $_GPC['type'];
    if ($type !== 'all'){
        $start_time = $_GPC['start_time'];
        $end_time = $_GPC['end_time'];
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where.=' and createtime > '.$start_time .' and createtime<='.$end_time;
    }
    $branch_info = pdo_get('yzzc_sun_branch',['uniacid'=>$_W['uniacid'],'id'=>$branch_id],['id','name','balance']);
    $sql = 'SELECT * FROM ' . tablename('branch_shuttle.inc.php') . " where {$where}";

    $info = pdo_fetchall($sql);
    if (!$info){
        message('暂无结算收益','','error');
    }
    $price = 0;
    foreach ($info as $value){
        $price+=$value['amount'];
    }

    $branch_update = pdo_update('yzzc_sun_branch',['balance' => $branch_info['balance'] + $price],['uniacid'=>$_W['uniacid'],'id'=>$branch_id]);
    $update_sql = 'UPDATE ' . tablename('branch_shuttle.inc.php') . " set status= 2,income_time = ".time()." where {$where}";
    $income_update = pdo_query($update_sql);
    if ($branch_update && $income_update){
        message('结算成功','','success');
    }
    message('结算失败，请联系管理员','','error');
}
if ($_GPC['op'] == 'change') {
    if ($_GPC['states'] == 2) {
        $is = pdo_get('yzzc_sun_goods', array('sid' => $_GPC['id'], 'status' => 1));
        if ($is) {
            message('请先至车辆列表下架所有车辆才能停业', '', 'error');
        }
    }

    $res = pdo_update('yzzc_sun_branch', array('status' => $_GPC['states']), array('id' => $_GPC['id']));
    if ($res) {
        message('操作成功', $this->createWebUrl('branchslist', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }


}

include $this->template('web/shuttle/income');